package com.hzncc.flowable_diboot.config;

import com.diboot.core.util.S;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;

import java.io.IOException;

public class MethodChangeFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(httpRequest) {
            @Override
            public String getMethod() {
                String method = httpRequest.getMethod();
                String mark = httpRequest.getHeader("mark");
                if (S.isNotBlank(mark) && S.equals(method.toUpperCase(), "POST")) {
                    return mark.toUpperCase();
                }
                return super.getMethod(); // 修改为新的HTTP方法
            }
        };
        chain.doFilter(requestWrapper, response);
    }

}